<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1987-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation.  A copy of
the license is included in the
section entitled "GNU Free Documentation License".

This manual contains no Invariant Sections.  The Front-Cover Texts are
(a) (see below), and the Back-Cover Texts are (b) (see below).

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Function-like Macros (The C Preprocessor)</title>

<meta name="description" content="Function-like Macros (The C Preprocessor)">
<meta name="keywords" content="Function-like Macros (The C Preprocessor)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Index-of-Directives.html" rel="index" title="Index of Directives">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Macros.html" rel="up" title="Macros">
<link href="Macro-Arguments.html" rel="next" title="Macro Arguments">
<link href="Object_002dlike-Macros.html" rel="prev" title="Object-like Macros">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span.r {font-family: initial; font-weight: normal; font-style: normal}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en">
<div class="section-level-extent" id="Function_002dlike-Macros">
<div class="nav-panel">
<p>
Next: <a href="Macro-Arguments.html" accesskey="n" rel="next">Macro Arguments</a>, Previous: <a href="Object_002dlike-Macros.html" accesskey="p" rel="prev">Object-like Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Function_002dlike-Macros-1"><span>3.2 Function-like Macros<a class="copiable-link" href="#Function_002dlike-Macros-1"> &para;</a></span></h3>
<a class="index-entry-id" id="index-function_002dlike-macros"></a>

<p>You can also define macros whose use looks like a function call.  These
are called <em class="dfn">function-like macros</em>.  To define a function-like macro,
you use the same &lsquo;<samp class="samp">#define</samp>&rsquo; directive, but you put a pair of
parentheses immediately after the macro name.  For example,
</p>
<div class="example smallexample">
<pre class="example-preformatted">#define lang_init()  c_init()
lang_init()
     &rarr; c_init()
</pre></div>

<p>A function-like macro is only expanded if its name appears with a pair
of parentheses after it.  If you write just the name, it is left alone.
This can be useful when you have a function and a macro of the same
name, and you wish to use the function sometimes.
</p>
<div class="example smallexample">
<pre class="example-preformatted">extern void foo(void);
#define foo() /* <span class="r">optimized inline version</span> */
...
  foo();
  funcptr = foo;
</pre></div>

<p>Here the call to <code class="code">foo()</code> will use the macro, but the function
pointer will get the address of the real function.  If the macro were to
be expanded, it would cause a syntax error.
</p>
<p>If you put spaces between the macro name and the parentheses in the
macro definition, that does not define a function-like macro, it defines
an object-like macro whose expansion happens to begin with a pair of
parentheses.
</p>
<div class="example smallexample">
<pre class="example-preformatted">#define lang_init ()    c_init()
lang_init()
     &rarr; () c_init()()
</pre></div>

<p>The first two pairs of parentheses in this expansion come from the
macro.  The third is the pair that was originally after the macro
invocation.  Since <code class="code">lang_init</code> is an object-like macro, it does not
consume those parentheses.
</p>
</div>



</body>
</html>
